package org.fh;

import io.netty.channel.ChannelHandlerContext;
import org.fh.cmd.CommunicationCmd;
import org.fh.domain.DataFrame;
import org.fh.domain.TcpData;
import org.fh.domain.Word;
import org.fh.inductionLamp.StatusHandle;


import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 数据处理
 * @Author: wwy
 * @Date: 2020/10/21 16:06
 */
public class HandleData {
    /**
     * 异步io响应同步处理
     * 目前一个连接，同时只能执行一个命令，不能执行多个
     * 根据控制器id查找当前返回的数据对象，处理完毕后必须删除该对象
     */
    public static HashMap<String, DataFrame> tcpDataHashMap = new HashMap<>();
    public StatusHandle statusHandle = new StatusHandle();


    //private int cmdHandle = 1;
    /**
     * 保存控制器编号与连接
     */
    public static ConcurrentHashMap<String,ChannelHandlerContext> channelHandlerContextMaps = new ConcurrentHashMap<>();

    public static Set<String> connectList = new HashSet<>();
    /**
     * 处理数据方法
     */
    public void dispose(ChannelHandlerContext ctx,String msg){
        if(msg == null || "".equals(msg)){
            return;
        }
        //channelHandlerContextMaps.put("60928",ctx);
        //dataService.getConfig("");
        DataFrame dataFrame = dataHandle(msg);
        String header = dataFrame.getHead().highLow;//帧头
        if(!"aa55".equalsIgnoreCase(header)){
            return;
        }

        String cmd = dataFrame.getCmd().highLow;
        int cmdInt = Integer.parseInt(cmd, 16);
        String srcId = dataFrame.getSrcID().highLow;
        int srcIdInt = Integer.parseInt(srcId, 16);
        TcpData tcpData = new TcpData();
        tcpData.setCmd(cmdInt);
        tcpData.setData(msg);
        if(cmdInt == CommunicationCmd.CMD_UPLOAD){
            //放入全局状态对象中
            //状态信息处理
            statusHandle.handle(dataFrame);
            return;
        }
        if(cmdInt == CommunicationCmd.CMD_CONNECT){
            //控制器连接命令
            connectList.add(String.valueOf(srcIdInt));
        }
        //放入tcp控制器返回的数据中
        tcpDataHashMap.put(String.valueOf(srcIdInt),dataFrame);
        channelHandlerContextMaps.put(String.valueOf(srcIdInt),ctx);

    }

    /**
     * 数据处理
     * @param msg
     * @return
     */
    public DataFrame dataHandle(String msg){
        DataFrame dataFrame = new DataFrame();
        String header = msg.substring(0, 4);
        String length = msg.substring(4, 8);
        String cmd = msg.substring(8, 12);
        String srcID = msg.substring(12, 16);
        String destID = msg.substring(16, 20);
        String number = msg.substring(20, 24);
        String data = msg.substring(24, msg.length());
        dataFrame.setHead(strConvert(header,false));
        dataFrame.setLength(strConvert(length,true));
        dataFrame.setCmd(strConvert(cmd,true));
        dataFrame.setSrcID(strConvert(srcID,true));
        dataFrame.setDestID(strConvert(destID,true));
        dataFrame.setNumber(strConvert(number,true));
        dataFrame.setDataStr(data);
        return dataFrame;

    }

    /**
     * 字符串16进制2字节高低位转换
     * 此时字符串是高位在前，低位在后
     * @param str
     */
    public Word strConvert(String str,boolean isLowHighConvert){
        Word word = new Word();
        if(isLowHighConvert){
            //进行位置还原
            String low = str.substring(0, 2);
            String high = str.substring(2,4);
            word = new Word(low,high);
        }else{
            //位置不动
            String high = str.substring(0, 2);
            String low = str.substring(2,4);
            word = new Word(low,high);
        }


        return word;

    }
}
